<template>
  <div class="main">
    <!-- <div style="width: 100%;height: 30px;display: flex;">
            <div style="width: 250px; display: flex">
                <el-button type="primary" size="mini" round @click="clean"></el-button>
                <el-button type="info" size="mini" round @click="submit"></el-button>
            </div>
        </div> -->

    <div v-if="userAll">
      <el-table :data="list" v-loading="queryLoading" style="width: 100%;
      height: 76vh;" :default-sort="{ prop: 'date', order: 'descending' }" key="table1">
        <el-table-column type="selection" width="55px" v-if="checkBox">
        </el-table-column>
        <el-table-column label="ID" prop="id" key="id">
          <template slot-scope="scope">
            <span style="">{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="订单号" prop="number" align="center">
          <template slot-scope="scope">
            <span style="">{{ scope.row.number }}</span>
          </template>
        </el-table-column>
        <el-table-column label="金额" prop="amount" align="center">
          <template slot-scope="scope">
            <span style="">{{ scope.row.amount }}￥</span>
          </template>
        </el-table-column>
        <el-table-column label="结算" prop="amount" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handle(scope.$index, scope.row)"
              style="width: 60px;height: 40px;">支付</el-button>
          </template>
        </el-table-column>
        <el-table-column label="收货人" prop="consignee" sortable align="center" width="190px">
          <template slot-scope="scope">
            <span style=""> {{ scope.row.consignee }}</span>
            <!-- <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" active-text="起售" inactive-text="停售"
                active-color="#13ce66" inactive-color="#ff4949" @change="dishStatus" ref="status">
              </el-switch> -->
          </template>
        </el-table-column>
        <el-table-column align="center" width="150px" label="操作">
          <template slot-scope="scope">
            <!-- <el-button size="medium" type="primary" @click="handleAdd(scope.$index, scope.row)"
                style="width: 60px;height: 40px;">加入购物车</el-button> -->
            <el-button size="medium" type="success" @click="handleInfo(scope.$index, scope.row)"
              style="width: 60px;height: 40px;">订单详情</el-button><el-button size="medium" type="danger"
              @click="handleDelete(scope.$index, scope.row)" style="width: 60px;height: 40px;">取消订单</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <dishInfoView :dialogVisible.sync="dialogVisible" :dialogInfo.sync="dialogInfo"></dishInfoView>
    <el-pagination :total="pagination.total" :current-page.sync="pagination.currentPage"
      :page-size.sync="pagination.PageSize" :page-sizes="[6]" layout="total, sizes, prev, pager, next, jumper"
      @size-change="query()" @current-change="query()" />
  </div>
</template>
<script>
import { METHOD, request } from '@/utils/request'
import { Message } from 'element-ui'
import dishInfoView from '../user/dishInfoView.vue'
// import Axios from 'axios'
export default {
  name: 'orderView',
  components: {
    dishInfoView
  },
  created () {
    this.query()
  },
  watch: {
    dialogInfo: {
      handler (newVal, oldVal) {
        const params = JSON.stringify(newVal)
        console.log(params)
        this.Editquery(params)
        // this.query()
      }
    }
  },
  // updated () {
  //   this.query()
  // },
  computed () {
    console.log(this.input)
  },
  mounted () {
  },
  data () {
    return {
      dialogVisible: false,
      dialogInfo: {},
      userAll: true,
      list: {},
      queryLoading: true, // 查询加载状态
      pagination: {
        // 分页数据
        total: 0, // 记录总数
        currentPage: 1, // 当前页
        PageSize: 6, // 每页的记录条数
        sort: 'updateAt,desc' // 查询排序依据
      }
    }
  },
  methods: {
    change () {
      this.$forceUpdate()
    },
    query () {
      this.userAll = true
      this.queryLoading = true
      this.userSearch = false
      const params = Object.assign(
        {
          page: this.pagination.currentPage,
          pageSize: this.pagination.PageSize
        })
      request('/api/user/order/conditionSearch', METHOD.POST, params).then((resp) => {
        this.list = resp.data.data.records
        this.pagination.total = resp.data.data.total
        this.currentPage = resp.data.data.currentPage
        this.pageSize = resp.data.data.pageSize
        this.queryLoading = false
        console.log(resp.data.data)
      })
    },
    // handleAdd (index, row) {
    //   const params = { dishId: row.id }
    //   console.log(row)
    //   request('/api/user/shoppingCart/add', METHOD.POST, params).then((resp) => {
    //     if (resp.status === 200) {
    //       Message({
    //         message: '加入购物车成功!',
    //         type: 'success',
    //         duration: 5 * 1000
    //       })
    //     }
    //   })
    // },
    handleInfo (x, y) {
      this.dialogVisible = true
      const params = y.id
      request('/api/user/order/orderDetail', METHOD.GET, params).then((resp) => {
        if (resp.status === 200) {
          Message({
            message: '查看订单!',
            type: 'info',
            duration: 2 * 1000
          })
          this.dialogInfo = resp.data.data
        }
      })
    },
    submit () {
      const value = this.list
      let sum = 0
      for (let i = 0; i < value.length; i++) {
        sum += value[i].amount
      }
      const params = {
        addressBookId: 2,
        remark: '订单',
        amount: sum
      }
      request('/api/user/shoppingCart/submit', METHOD.POST, params).then((resp) => {
        if (resp.status === 200) {
          Message({
            message: '提交订单!',
            type: 'success',
            duration: 5 * 1000
          })
        }
      })
    },
    handleDelete (x, y) {
      request(`/api/user/order/deleteOrder/${y.id}`, METHOD.DELETE).then((resp) => {
        if (resp.status === 200) {
          Message({
            message: '取消订单成功!',
            type: 'success',
            duration: 2 * 1000
          })
          this.query()
        }
      })
    },
    handle () {
      Message({
        message: '支付成功!',
        type: 'success',
        duration: 2 * 1000
      })
    },
    dishStatus () {
      console.log(this.$refs.status.value)
    },
    categoryIdSearch () {

    }
  }
}
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
}

.el-pagination {
  margin-left: 25%;
  width: 200px;
  height: 50px;
}
</style>
